$(function () {
    var demoData = [{ ID: "1", PID: null, NAME: "root", ISLEAF: "0" },
        { ID: "11", PID: 1, NAME: "LEAF1", ISLEAF: "1", URL: "sipcquery.html?node=zhtj" },
        { ID: "2", PID: null, NAME: "root2", ISLEAF: "0" },
        { ID: "21", PID: 2, NAME: "LEAF21", ISLEAF: "1" },
        { ID: "22", PID: 2, NAME: "LEAF22", ISLEAF: "0" },
        { ID: "23", PID: 2, NAME: "LEAF23", ISLEAF: "1" },
        { ID: "221", PID: 22, NAME: "LEAF221", ISLEAF: "1" },
        { ID: "222", PID: 22, NAME: "LEAF222", ISLEAF: "1" }
    ];
    function getSubMenuinfo(pid) {
        var arr = [];
        $.each(demoData, function (i, menuinfo) {
            if (menuinfo.PID == pid) {
                arr.push(menuinfo);
            }
        });
        return arr;
    }
    function renderMenuItem(menuInfo, level) {
        if (menuInfo.ISLEAF == "1") {
            return "<li><a data-id='" + menuInfo.ID + "' data-url='" + menuInfo.URL + "'>" + menuInfo.NAME + "</a></li>";
        }
        else {
            var retStr = '';
            if (level == 0) {
                retStr = '<li class="dropdown">';
                retStr += "<a href=\"#\" data-id=\"" + menuInfo.ID + "\" data-toggle=\"dropdown\" data-submenu >" + menuInfo.NAME + " <span class='caret'></span></a>";
            }
            else {
                retStr = '<li class="dropdown-submenu">';
                retStr += "<a href=\"#\" data-id=\"" + menuInfo.ID + "\"> " + menuInfo.NAME + " </a>";
            }
            retStr += '<ul class="dropdown-menu">';
            $.each(getSubMenuinfo(menuInfo.ID), function (i, info) {
                retStr += renderMenuItem(info, level + 1);
            });
            retStr += '</ul></li>';
            return retStr;
        }
    }
    function renderMenu() {
        var retStr = "";
        $.each(getSubMenuinfo(null), function (i, info) {
            retStr += renderMenuItem(info, 0);
        });
        $("#ulMenu").append(retStr);
        $('[data-submenu]').submenupicker();
    }
    renderMenu();
    $("#ulMenu").on("click", "[data-url]", function () {
        var $a = $(this);
        window.addFrameTab($a.text(), $a.data("url"));
    });
    window["CurTreeNode"] = demoData[0];
    var setting = {
        data: {
            key: {
                name: "NAME",
                title: "NAME"
            },
            simpleData: {
                enable: true,
                idKey: "ID",
                pIdKey: "PID"
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode) {
                window["CurTreeNode"] = treeNode;
                $("iframe").each(function (i, frm) {
                    frm.contentWindow.postMessage(JSON.stringify(treeNode), "*");
                });
            }
        }
    };
    $.fn.zTree.init($("#ulOrgTree"), setting, demoData);
    $('#topTabs').tabs({
        fit: true,
        border: false
    });
    window.addFrameTab = function (title, url) {
        if ($('#topTabs').tabs('exists', title)) {
            var tab = $('#topTabs').tabs('getTab', title);
            tab.find("iframe").attr("src", url);
            $('#topTabs').tabs('select', title);
        }
        else {
            var _content = $('<iframe  scrolling="auto" ></iframe>');
            $('#topTabs').tabs('add', { title: title, content: _content[0], url: url, closable: true, closed: true, fit: true });
            _content.attr('id', 'tab_' + title).attr('title', title).attr('src', url).attr('frameborder', '0').css({ width: "100%", height: "100%" });
        }
    };
    window.addFrameTab("首页", "sipcquery.html?node=zhtj");
    $('#topTabs').on('contextmenu', '.tabs-inner', function (e) {
        $('#mm').menu('show', { left: e.pageX, top: e.pageY });
        $('#mm').data("currtab", $(this).children("span").text());
        return false;
    }).on('dblclick', '.tabs-inner', function () {
        $('#topTabs').tabs('close', $(this).children(".tabs-title").text());
    });
    $('#mm-tabclose').click(function () {
        $('#topTabs').tabs('close', $('#mm').data("currtab"));
    });
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            if ($(n).hasClass('tabs-closable') == false)
                return;
            $('#topTabs').tabs('close', $(n).text());
        });
    });
    $('#mm-tabcloseother').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            if ($(n).hasClass('tabs-closable') == false)
                return;
            if ($(n).text() == $('#mm').data("currtab"))
                return;
            $('#topTabs').tabs('close', $(n).text());
        });
    });
    $('#mm-tabcloseright').click(function () {
        var count = 0, nextall = $('.tabs-selected').nextAll();
        nextall.each(function (i, n) {
            if ($('.tabs-title', $(n)).hasClass('tabs-closable') == false)
                return;
            $('#topTabs').tabs('close', $('a:eq(0) span', $(n)).text());
            count = count + 1;
        });
        return false;
    });
    $('#mm-tabcloseleft').click(function () {
        var count = 0, prevall = $('.tabs-selected').prevAll();
        prevall.each(function (i, n) {
            if ($('.tabs-title', $(n)).hasClass('tabs-closable') == false)
                return;
            $('#topTabs').tabs('close', $('a:eq(0) span', $(n)).text());
            count = count + 1;
        });
        return false;
    });
});
